package o2sjsc.activity;

import java.util.HashMap;

import o2sjsc.core.libs.json.JSONParser;
import o2sjsc.core.libs.soap.SoapUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.TextSize;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class ContentDetailAsynTask extends AsyncTask<Void, Void, Void> {

	/** Declare variable */
	private static final String METHOD_NAME = "MGetContentById";
	private static final String URL = "http://webbi.vn/WebServices/TechsService.asmx";

	Activity activity;
	TextView tvTitle;
	//tvLableCreateDate, tvCreateDate;
	WebView wvContentDetail;
	HashMap<String, String> paramInput;
	int widthLayoutTechS;

	private static String CONTENT_DETAIL = "CONTENT_DETAIL";
	private static String FULL_TITLE = "FULL_TITLE";
	private static String CREATE_DATE = "CREATE_DATE";
	private static String REWRITE_URL = "REWRITE_URL";
	private static String THUMB_IMG = "THUMB_IMG";

	public ContentDetailAsynTask(Activity activity, String contentId,
			TextView tvTitle, /*TextView tvLbCreateDate, TextView tvCreatDate,*/
			WebView wvContent) {
		this.activity = activity;
		paramInput = new HashMap<String, String>();
		paramInput.put("contentId", contentId);
	this.tvTitle = tvTitle;
	/*	this.tvLableCreateDate = tvLbCreateDate;
		this.tvCreateDate = tvCreatDate;*/
		this.wvContentDetail = wvContent;
		Display display = activity.getWindowManager().getDefaultDisplay();
		// Display display =
		// activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		widthLayoutTechS = display.getWidth();
	}

	private int getScale() {
		int PIC_WIDTH = wvContentDetail.getRight() - wvContentDetail.getLeft();
		Double val = new Double(widthLayoutTechS) / new Double(PIC_WIDTH);
		val = val * 100d;
		return val.intValue();
	}

	private String strTitle = "";
	private String strContentDetal = "";
	private String strCreateDate = "";
	private String strRewriteUrl="";
	private String strThum="";

	ProgressDialog progressDialog;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.AsyncTask#onPreExecute()
	 */
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progressDialog = new ProgressDialog(activity);
		//progressDialog.setTitle(ValueConstants.TITLE_LOADING);
		progressDialog.setMessage(ValueConstants.LOADING);
		progressDialog.show();
	}

	@Override
	protected Void doInBackground(Void... params) {
		String jsonString = SoapUtils.Invoke(URL, METHOD_NAME, paramInput);
		try {
			JSONParser jsonParser = new JSONParser();
			JSONArray json = jsonParser.getJSONFromString(jsonString);

			JSONObject c = json.getJSONObject(0);

			strTitle = c.getString(FULL_TITLE);
			strContentDetal = c.getString(CONTENT_DETAIL);
			strCreateDate = c.getString(CREATE_DATE);
			strRewriteUrl = c.getString(REWRITE_URL);
			strThum = c.getString(THUMB_IMG);
			String[] src = strContentDetal.split("font-size");
			strContentDetal = "";
			for (int i = 0; i < src.length; i++) {
				strContentDetal = strContentDetal + src[i];
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
	 */
	@Override
	protected void onPostExecute(Void result) {
		Global.Link=strRewriteUrl;
		Global.Thumb_Img=strThum;
		tvTitle.setText(strTitle);
		/*tvLableCreateDate.setText(ValueConstants.CREATE_DATE);
		tvCreateDate.setText(strCreateDate);*/
		String strResizeImage = "<head><meta name='viewport' content='user-scalable=no,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, width=device-width, height=device-height, target-densitydpi=device-dpi' />" +
				"<title>Example</title>" +
				"<style type='text/css'>img{display:none}; body {font-family:sans-serif,Verdana, Geneva, Tahoma;}</style></head>";
		String strTitleHead ="<h6 style='color:black;'>"+(ValueConstants.CREATE_DATE)+strCreateDate+"</h6><h4 style='color:red;'>"+strTitle+"</h4>";
				//<p/><p/>
				strContentDetal ="<html>"+ strResizeImage+"<body>"+strTitleHead+strContentDetal+"</body></html>";
		//strResizeImage += strContentDetal;
		wvContentDetail.setPadding(0, 0, 0, 0);
		/* wvContentDetail.setInitialScale(getScale()); */
		wvContentDetail
				.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
		wvContentDetail.setScrollbarFadingEnabled(false);// Needs api 5
		WebSettings webSettings = wvContentDetail.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setTextSize(TextSize.LARGER);
	//	webSettings.setDomStorageEnabled(true);
		webSettings.setLoadWithOverviewMode(true);
		webSettings.setUseWideViewPort(true);
		webSettings.setDefaultZoom(ZoomDensity.FAR);
		 webSettings.setPluginState(WebSettings.PluginState.ON);
		 wvContentDetail.requestFocusFromTouch();
		wvContentDetail.loadDataWithBaseURL(null,strContentDetal, "text/html","utf-8",null);
		final String js = "javascript:(function () {var w = "
				+ widthLayoutTechS
				+ ";for( var i = 0; i < document.images.length; i++ ) {var img = document.images[i];if( img.width > w ) {img.height = Math.round( img.height * ( w/img.width ) );img.width = w;img.style.display='block';};}})();";
		wvContentDetail.setWebViewClient(new WebViewClient() {
			@Override
	        public boolean shouldOverrideUrlLoading(WebView view, String url) {
	            view.loadUrl(url);
	            return true;
	        }
			@Override
			public void onPageFinished(WebView view, String url) {
				view.loadUrl(js);
				//view.loadUrl(jsRemoveFontSize);
			}
		});
		progressDialog.dismiss();
	}
}
